﻿#include "mainwindow.h"
#include<QSize>
#include<QString>
#include<QLabel>
#include<QDebug>
using std::array;
bool MainWindow::construct()
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    ret=  initilalMenu();
    ret = initialToolBar();
    ret = initialMianEdit();
    ret = initialStutusBar();
    qDebug()<<__LINE__<<__FUNCSIG__;
    connect(&m_edit,SIGNAL(cursorPositionChanged()),this,SLOT(cursorChange()));
    return ret;

}

bool MainWindow::initilalMenu()
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    QMenuBar *mb = menuBar();
    array<QString,5>menu={
        tr("File(&N)"),
        tr("Edit(&E)"),
        tr("Format(&F)"),
        tr("View(&V)"),
        tr("Help(&H)"),

    };

    if(mb)
    {
        initialFile(mb,menu[0]);
        initialEdit(mb,menu[1]);
        initialFormat(mb,menu[2]);
        initialView(mb,menu[3]);
        initialHelp(mb,menu[4]);
    }
    else
    {
        ret = false;
    }
    return ret;
}

bool MainWindow::makeAction(QAction *&action, QWidget *parent, QString tips, const QString icon)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    action = new QAction(parent); // 初始化  toolbar
    if(action)
    {
        action->setToolTip(tips); // 设置鼠标悬停的tips
        action->setIcon(QIcon(icon)); //设置icon
    }
    else
    {
        ret = false;
    }
    return ret;
}

bool MainWindow::makeAction(QAction *&action, QWidget *parent, QString text, int key)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    action = new QAction(parent);
    action->setText(text);
    if(action)
    {
        action->setShortcut(key);
    }
    else
    {
        ret = false;
    }
    return ret;
}

bool MainWindow::initialFile(QMenuBar *mb,const QString menuName)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    QMenu *menu=new QMenu(menuName,mb);

    const  array<QString,7>text={
        "new",
        "open",
        "save",
        "saveas",
        "format",
        "print",
        "exit",

    };

    const array<int, 7>key={
        Qt::CTRL+Qt::Key_N,
        Qt::CTRL+Qt::Key_O,
        Qt::CTRL+Qt::Key_S,
        Qt::CTRL+Qt::Key_M,
        0,
        Qt::CTRL+Qt::Key_P,
        0,

    };


    array<QAction * ,7>action={
        nullptr,
    };
    for(unsigned int i = 0; i< action.size(); ++i)
    {
        makeAction(action[i],menu,text[i],key[i]);
        if(action[i])
        {
            menu->addAction(action[i]);
        }
        else
        {
            ret  = false;
            return ret;

        }
        // 添加一个分隔符号
        if(i == 3 || i == 5)
        {
            menu->addSeparator();
        }
    }

    connect(action[0],SIGNAL(triggered()),this,SLOT(onFileNew()));
    connect(action[1],SIGNAL(triggered()),this,SLOT(onFileOpen()));
    connect(action[2],SIGNAL(triggered()),this,SLOT(onFileSave()));
    connect(action[3],SIGNAL(triggered()),this,SLOT(onFileSaveAs()));
    connect(action[4],SIGNAL(triggered()),this,SLOT(onFontChange()));

    connect(action[5],SIGNAL(triggered()),this,SLOT(onFilePrint()));
    connect (action[6],SIGNAL(triggered()),this,SLOT(onFileExit()));
    mb->addMenu(menu);

    return ret;
}

bool MainWindow::initialEdit(QMenuBar *mb, const QString menuName)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret =true;


    QMenu * menu = new QMenu(menuName,mb);

    const array<QString ,11>text={
        "undo(U)",
        "cut(T)",
        "copy(C)",
        "paste(P)",
        "delete(L)",
        "find(F)",
        "find next(N)",
        "replace(R)",
        "goto(G)",
        "select all(A)",
        "date(D)",
    };

    const array<int, 11>key={
        Qt::CTRL+Qt::Key_Z,
        Qt::CTRL+Qt::Key_X,
        Qt::CTRL+Qt::Key_C,
        Qt::CTRL+Qt::Key_V,
        Qt::Key_Delete,
        Qt::CTRL+Qt::Key_F,
        Qt::Key_F3,

        Qt::CTRL+Qt::Key_H,

        Qt::CTRL+Qt::Key_G,
        Qt::CTRL+Qt::Key_A,
        Qt::Key_F5,


    };

    array<QAction * ,11>action={
        nullptr,
    };

    for(unsigned int i = 0; i< action.size(); ++i)
    {
        makeAction(action[i],menu,text[i],key[i]);
        if(action[i])
        {
            menu->addAction(action[i]);
        }
        else
        {
            ret  = false;
            return ret;

        }
        // 添加一个分隔符号
        if(i == 0 || i == 8 || i == 4)
        {
            menu->addSeparator();
        }
    }



    connect(action[0],SIGNAL(triggered()),&m_edit,SLOT(undo()));
    connect(action[1],SIGNAL(triggered()),&m_edit,SLOT(cut())); // 剪切
    connect(action[2],SIGNAL(triggered()),&m_edit,SLOT(copy())); //复制
    connect(action[3],SIGNAL(triggered()),&m_edit,SLOT(paste())); // 粘贴
    connect(action[9],SIGNAL(triggered()),&m_edit,SLOT(selectAll())); // 全选

    connect(action[4],SIGNAL(triggered()),this,SLOT(onEditDelete()));
    connect(action[5],SIGNAL(triggered()),this,SLOT(onFindEdit()));
    action[0]->setEnabled(false);
    action[1]->setEnabled(false);
    action[2]->setEnabled(false);
    //action[4]->setEnabled(false);
    action[9]->setEnabled(false);


    mb->addMenu(menu);

    return ret;
}

bool MainWindow::initialFormat(QMenuBar *mb, const QString menuName)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret  = true;
    QMenu * menu = new QMenu(menuName,mb);
    const array<QString ,2>text={

        "auto change(&W)",
        "bold(&F)",
    };

    array<QAction *, 2> action={
        nullptr,
    };
    const array<int , 2 > key={
        0,
        0,
    };
    for(unsigned int i = 0; i< action.size(); ++i)
    {
        makeAction(action[i],menu,text[i],key[i]);
        if(action[i])
        {
            menu->addAction(action[i]);
        }
        else
        {
            ret  = false;
            return ret;

        }

    }

    mb->addMenu(menu);
    return  ret;
}

bool MainWindow::initialView(QMenuBar *mb, const QString menuName)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret  = true;
    QMenu * menu = new QMenu(menuName,mb);

    QAction * action = nullptr;
    QString text  = "status(&s)";
    makeAction(action,menu,text,0);
    if(action)
    {
        menu->addAction(action);
        connect(action,SIGNAL(triggered(bool)),this,SLOT(onStatusVisual()));
    }
    else
    {
        ret = false;
        return false;
    }

    action->setCheckable(true);
    action ->setChecked(true);
    mb->addMenu(menu);
    return  ret;
}

bool MainWindow::initialHelp(QMenuBar *mb, const QString menuName)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret  = true;
    QMenu * menu = new QMenu(menuName,mb);

    const array<QString ,2>text={

        "help(H)",
        "about myNotePad(A)",
    };

    array<QAction *, 2> action={
        nullptr,
    };
    const array<int , 2 > key={
        0,
        0,
    };
    for(unsigned int i = 0; i< action.size(); ++i)
    {
        makeAction(action[i],menu,text[i],key[i]);
        if(action[i])
        {
            menu->addAction(action[i]);
        }
        else
        {
            ret  = false;
            return ret;

        }

    }

    mb->addMenu(menu);
    return  ret;
}

bool MainWindow::initialToolBar()
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    QToolBar *tb = addToolBar("Tool Bar");
    tb->setFloatable(true);
    tb->setMovable(false);
    tb->setIconSize(QSize(16,16));
    initialFileToolBar(tb);
    initialEditToolBar(tb);

    return ret;
}

bool MainWindow::initialFileToolBar(QToolBar *tb)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;

    const array<QString,7> tips={
        "new",
        "open",
        "save",
        "saveas",
        "close",
        "close all",
        "print",

    };

    const array<QString, 7>icon={
        ":img/toolbar/new.png",
        ":img/toolbar/open.png",
        ":img/toolbar/save.png",

        ":img/toolbar/saveas.png",
        ":img/toolbar/close.png",
        ":img/toolbar/closeall.png",
        ":img/toolbar/print.png",


    };

    array<QAction* , 7> action={
        nullptr,
    };

    for(unsigned int i = 0 ; i< action.size(); ++i)
    {
        makeAction(action[i],this,tips[i],icon[i]);
        if(action[i])
        {
            tb->addAction(action[i]);
        }
        else
        {
            ret = false;
            return ret;
        }
    }

    // 加入分割符号
    tb->addSeparator();

    connect(action[3],SIGNAL(triggered()),this,SLOT(onFileSaveAs()));

    connect(action[2],SIGNAL(triggered()),this,SLOT(onFileSave()));

    connect(action[1],SIGNAL(triggered()),this,SLOT(onFileOpen()));
    connect(action[0],SIGNAL(triggered()),this,SLOT(onFileNew()));
    connect(action[6],SIGNAL(triggered()),this,SLOT(onFilePrint()));


    return ret;
}

bool MainWindow::initialEditToolBar(QToolBar *tb)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    const array<QString,6> tips ={
        "cut",
        "copy",
        "paste",
        "find",
        "replace",
        "undo",
    };

    const array<QString ,6>icon={
        ":img/toolbar/cut.png",
        ":img/toolbar/copy.png",
        ":img/toolbar/paste.png",
        ":img/toolbar/find.png",
        ":img/toolbar/replace.png",
        ":img/toolbar/undo.png",

    };

    array<QAction * , 6> action={
        nullptr,
    };

    for(unsigned int i = 0 ; i< action.size(); ++i)
    {
        makeAction(action[i],this,tips[i],icon[i]);
        if(action[i])
        {
            tb->addAction(action[i]);
        }
        else
        {
            ret = false;
            return ret;
        }
    }
    connect(action[0],SIGNAL(triggered()),&m_edit,SLOT(cut())); // 剪切
    connect(action[1],SIGNAL(triggered()),&m_edit,SLOT(copy())); //复制

    connect(action[2],SIGNAL(triggered()),&m_edit,SLOT(paste())); // 粘贴

    connect(action[5],SIGNAL(triggered()),&m_edit,SLOT(undo())); // 撤销

    action[0]->setEnabled(false);
    action[5]->setEnabled(false);
    // 加入分割符号

    tb->addSeparator();
    return ret;
}

bool MainWindow::initialFormatToolBar(QToolBar *tb)
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    // const array<QString ,

    return ret;
}

bool MainWindow::initialViewToolBar(QToolBar *tb)
{
    bool ret =true;
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;


    return ret;
}

bool MainWindow::initialStutusBar()
{
    bool ret =true;
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    QStatusBar *sb = statusBar();
    sb->setVisible(true);
    // QWidget * wid = new QWidget(this);

    if(sb)
    {
        ;
        m_label.setText("ln : 1    col : 1");
        m_label.setMinimumWidth(150);
        m_label.setAlignment(Qt::AlignCenter);
        sb->setMinimumWidth(300);
        sb->addWidget(&m_label);
        sb->insertWidget(1,&m_word);
        m_word.setText("word : "+QString::number(0));
    }
    else
    {
        ret = false;
    }
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    return ret;
}

bool MainWindow::initialMianEdit()
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    bool ret = true;
    m_edit.setParent(this);
    QFont font;
    /*
     * 设置字体的大小默认字体为14 号， 设置字体不是为

      */
    font.setBold(false);
    font.setPointSize(14);

    m_edit.setFont(font);

    setCentralWidget(&m_edit);                              // 设置中央界面
    connect(&m_edit,SIGNAL(textChanged()),this,SLOT(onTextChange()));

    connect(&m_edit,SIGNAL(copyAvailable(bool)),this,SLOT(onCopyAvailable(bool)));
    connect(&m_edit,SIGNAL(undoAvailable(bool)),this,SLOT(onUndoAvailable(bool)));
    connect(&m_edit,SIGNAL(cursorPositionChanged()),this,SLOT(onCursorPositionChange()));
    return ret;
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),m_edit(this), m_label(this),m_word(this),m_pFindDlg (new FindDialog(this,&m_edit))
{
    qDebug()<<__LINE__<<__FUNCSIG__;
    m_sbChange= false;                                      // 光标的位置是否异动
    m_file="";
    m_isTextChange = false;                                  // 设置文本 框的数据是否繁盛变化
    setAcceptDrops(true);                                   //设置为允许拖拉文件 。。 并且一定要设置为true
    m_edit.setAcceptDrops(false);                           // 这个一定要设置为false ....否则无法执行拖动文件的艰巨任务。。。。。
}

MainWindow *MainWindow::getNewInstance()
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

    MainWindow * ret = new MainWindow();
    qDebug()<<__LINE__;
    if(ret && ret ->construct())
    {
        // 做一些相应的操作

    }
    else
    {
        delete ret;
    }
    qDebug()<<__LINE__  ;
    return ret;
}

MainWindow::~MainWindow()
{
    qDebug()<<__LINE__<<__FUNCSIG__<<__FILE__;

}
